﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Weight.Dal.Contract;
using Weight.Model;
using Weight.Model.ParamEntity;

namespace Weight.Bll
{
    public class BllContractCustomer
    {
        private DalContractCustomer Dal = new DalContractCustomer();
        /// <summary>
        /// 获取订单客户信息列表
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public IEnumerable<ContractCustomerEntity> QueryList(QueryContractCustomerReq req)
        {
            if (req.IsValid == "作废") req.IsValid = "否";
            else if (req.IsValid == "激活") req.IsValid = "是";
            else req.IsValid = string.Empty;

            if (req.IsVip == "信用客户") req.IsVip = "是";
            else if (req.IsVip == "非信用客户") req.IsVip = "否";
            else req.IsVip = string.Empty;

            req.IsDel = "否"; //只检索非删除数据

            return   Dal.QueryList(req);
        }

        /// <summary>
        /// 根据Id查找订单客户信息
        /// </summary>
        /// <param name="id">订单客户Id</param>
        /// <returns></returns>
        public Tuple<bool,string, ContractCustomerEntity> GetById(string id)
        {
            if (string.IsNullOrEmpty(id)) return new Tuple<bool, string, ContractCustomerEntity>(false,"参数错误",null);
            var data = Dal.GetById(id);
            if(data != null)
            {
                return new Tuple<bool, string, ContractCustomerEntity>(true,"查询成功",data);
            }
            else
            {
                return new Tuple<bool, string, ContractCustomerEntity>(false, "查询成功", data);
            }
        }

        /// <summary>
        /// 添加订单客户信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public Tuple<bool,string> Add(ContractCustomerEntity entity)
        {
            if (entity == null) return new Tuple<bool, string>(false, "参数错误");
            if(Dal.IsExist(entity.CardId)) return new Tuple<bool, string>(false, "该客户信息已经存在，不能重复添加!");
            if (Dal.Add(entity))
            {
                return new Tuple<bool, string>(true, "添加成功");
            }
            else
            {
                return new Tuple<bool, string>(false, "添加失败");
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public Tuple<bool, string> Update(ContractCustomerEntity entity)
        {
            if (entity == null) return new Tuple<bool, string>(false, "参数错误");
            if(Dal.IsExist(entity.CardId,entity.Id)) return new Tuple<bool, string>(false, "客户信息已经存在，不能重复添加!");
            var oldEntity = Dal.GetById(entity.Id);
            if(oldEntity.IsValid=="否" || oldEntity.IsDel=="是")
            {
                return new Tuple<bool, string>(false, "已删除和作废的订单不能修改!");
            }
            if (Dal.Update(entity))
            {
                return new Tuple<bool, string>(true, "修改成功");
            }
            else
            {
                return new Tuple<bool, string>(false, "修改失败");
            }
        }

        /// <summary>
        /// 设置是否有效
        /// </summary>
        /// <param name="id"></param>
        /// <param name="isValid"></param>
        /// <returns></returns>
        public Tuple<bool, string> SetValid(string id, bool isValid)
        {
            if (string.IsNullOrWhiteSpace(id)) return new Tuple<bool, string>(false, "参数错误");
            if (Dal.SetValid(id,isValid))
            {
                return new Tuple<bool, string>(true, "设置成功");
            }
            else
            {
                return new Tuple<bool, string>(false, "设置失败");
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Tuple<bool, string> Del(string id)
        {
            if (string.IsNullOrWhiteSpace(id)) return new Tuple<bool, string>(false, "参数错误");
            if (Dal.Del(id))
            {
                return new Tuple<bool, string>(true, "删除成功");
            }
            else
            {
                return new Tuple<bool, string>(false, "删除失败");
            }
        }
    }
}
